using Core;
using UnityEngine;

namespace Modules.SoundEffect
{
    [RequireComponent(typeof(AudioSource))]
    public class SoundEffectPlayer : GameSubsystem
    {
        // 声音效果常量定义
        private const string CorrectSound = "正确"; // 正确音效
        private const string WrongSound = "错误"; // 错误音效
        private const string ClickSound = "点击"; // 点击音效
        private const string EnterSound = "进入"; // 进入音效
        private const string StartSound = "开始"; // 开始音效


        private AudioSource _player;
        private SoundEffectStore _soundEffectStore;

        protected override void Awake()
        {
            base.Awake();
            _player = GetComponent<AudioSource>();
            DontDestroyOnLoad(this);
            _soundEffectStore = Resources.Load<SoundEffectStore>(nameof(SoundEffectStore));
        }

        private void Play(string label)
        {
            if (_player.isPlaying)
                _player.Stop();

            _player.clip = _soundEffectStore.GetClip(label);
            _player.Play();
        }
        
        public float Volume
        {
            set => _player.volume = value;
            get => _player.volume;
        }

        public float Pitch
        {
            set => _player.pitch = value;
            get => _player.pitch;
        }
        
        
        /// <summary>
        /// 播放正确音效。
        /// </summary>
        public void PlayCorrect()
        {
            Play(CorrectSound);
        }

        /// <summary>
        /// 播放错误音效。
        /// </summary>
        public void PlayInCorrect()
        {
            Play(WrongSound);
        }

        /// <summary>
        /// 播放点击音效。
        /// </summary>
        public void PlayClick()
        {
            Play(ClickSound);
        }

        /// <summary>
        /// 播放进入音效。
        /// </summary>
        public void PlayEnter()
        {
            Play(EnterSound);
        }

        /// <summary>
        /// 播放开始音效。
        /// </summary>
        public void PlayStart()
        {
            Play(StartSound);
        }
    }
}